package alg20241203;

public class Text {
    public boolean canPartition(int[] nums) {
        int n = nums.length;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
        }
        //1 创建表
        boolean[][] dp = new boolean[n + 1][sum + 1];
        //2 初始化
        for (int i = 1; i <= n; i++) {
            dp[i][0] = true;
        }
        //3 填表
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= sum; j++) {
                dp[i][j] = dp[i - 1][j];
                if(j >= nums[i - 1]) dp[i][j] = dp[i - 1][j] || dp[i][j - nums[i - 1]];
            }
        }
        //4 返回值
        return dp[n][sum];
    }
}
